package com.example.studentcompetitionplatform.service.impl;

import com.example.studentcompetitionplatform.entity.Users;
import com.example.studentcompetitionplatform.mapper.UserMapper;
import com.example.studentcompetitionplatform.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Users login(Users users) {
        if (userMapper.selectUP(users)) {
            Users users1 = new Users();
            users1.setUsername(users.getUsername());
            return userMapper.selectR(userMapper.selectUI(users1));

        }
        return null;
    }

    @Override
    public void register(Users users) {
        Users users1 = new Users();
        users1.setUsername(users.getUsername());
       if(userMapper.selectU(users1)!=null){
           System.out.println("用户已存在");
           return;
       }else {
           userMapper.insertUser(users);
       }

    }

    @Override
    public void delete(Users users) {
        Users users1=new Users();
        users1.setUserId(users.getUserId());
        userMapper.deleteById(users1);
    }

    @Override
    public void update(Users users) {
       userMapper.update(users);
    }

    @Override
    public Map<String, Object> page(Integer pageNum, Integer pageSize, String username, String email, String phone) {
        pageNum = (pageNum - 1) * pageSize;
        if (username == "") {
            username = null;
        }
        if (email == "") {
            email = null;
        }
        if (phone == "") {
            phone = null;
        }
        List<Users> data = userMapper.selectPage(pageNum, pageSize, username, email, phone);
        Integer total = userMapper.findUserTotal(username);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

}
